using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 挂载在 canvas 上，管理对话面板
/// </summary>
public class DialogueUI : Singleton<DialogueUI>
{
    [Header("Basic Elements")]
    public Image icon;
    public Text mainText;
    public Button nextBtn;

    public GameObject dialoguePanel;
    [Header("Dialogue Option")]
    public RectTransform optionPanel;
    public OptionUI optionPrefab;
    [Header("Data")]
    public DialogueData_SO currentData;
    /// <summary>
    /// 默认从索引为 0 的那一条对话开始播放
    /// </summary>
    private int currentIndex = 0;

    protected override void Awake()
    {
        base.Awake();
        nextBtn.onClick.AddListener(ContinueDialogue);
    }

    public void ContinueDialogue()
    {
        if (currentIndex < currentData.dialoguePieces.Count)
        {
            UpdateMainDialogue(currentData.dialoguePieces[currentIndex]);
        }
        else
        {
            dialoguePanel.SetActive(false);
        }
    }
    /// <summary>
    /// 将 NPC 身上的对话数据传入 UI
    /// </summary>
    /// <param name="dialogueData"></param>
    public void UpdateDialogueData(DialogueData_SO dialogueData)
    {
        currentData = dialogueData;
        currentIndex = 0;
    }
    /// <summary>
    /// 显示对话主面板
    /// </summary>
    /// <param name="dialoguePiece"></param>
    public void UpdateMainDialogue(DialoguePiece dialoguePiece)
    {
        dialoguePanel.SetActive(true);

        currentIndex++;

        if (dialoguePiece.image != null)
        {
            icon.enabled = true;
            icon.sprite = dialoguePiece.image;
        }
        else
        {
            icon.enabled = false;
        }
        mainText.text = string.Empty;

        // mainText.text = dialoguePiece.text;

        mainText.DOText(dialoguePiece.text, 1f);

        // 没有对话选项 且 当前对话数据中至少有 1 条对话 时 显示 next 按钮
        if (dialoguePiece.dialogueOptions.Count == 0 && currentData.dialoguePieces.Count > 0)
        {
            nextBtn.interactable = true;
            nextBtn.transform.GetChild(0).gameObject.SetActive(true);
        }
        // 为了保持对话框的布局，不关闭按钮而是关闭文字并取消按钮互动
        else
        {
            nextBtn.interactable = false;
            nextBtn.transform.GetChild(0).gameObject.SetActive(false);
        }

        // 创建 options
        CreateOption(dialoguePiece);

        // 强制刷新 UI
        LayoutRebuilder.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
    }
    private void CreateOption(DialoguePiece dialoguePiece)
    {
        // 清零
        if (optionPanel.childCount > 0)
        {
            for (int i = 0; i < optionPanel.childCount; i++)
            {
                Destroy(optionPanel.GetChild(i).gameObject);
            }
        }

        for (int i = 0; i < dialoguePiece.dialogueOptions.Count; i++)
        {
            // 生成选项
            var option = Instantiate(optionPrefab, optionPanel);

            // 更新文字
            option.UpdateOptionUI(dialoguePiece, dialoguePiece.dialogueOptions[i]);
        }
    }
}
